package com.snow.utils;

import com.github.pagehelper.PageInfo;
import com.snow.constant.Webconst;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * 各个网页所需要数据的一些公共函数
 * @author: snow
 * @Date: 2020-01-19
 **/

@Component
public class Commons {
    /**
     * 网站配置项
     * @param key
     * @param defaultValue
     * @return
     */

    public static String site_option(String key,String defaultValue){
        if(StringUtils.isBlank(key)){
            return "";
        }
        String str = Webconst.initConfig.get(key);
        if(StringUtils.isBlank(str)){
            return defaultValue;
        }
        else{
            return str;
        }
    }
    public static String site_option(String key){
        return site_option(key,"");
    }
    public static String site_url(){
        return site_url("");
    }
    public static String site_url(String sub){
        return site_option("site_url" ) + sub;
    }

    public static String site_keywords(){
        return site_option("site_keywords");
    }
    public static String site_description(){
        return site_option("site_description");
    }
    public static String site_record(){
        return site_option("site_record");
    }
    public static String site_title(){
        return site_option("site_title");
    }
    /**
     * 获取GitHub地址
     * @return
     */
    public static String social_github(){
        return site_option("social_github");
    }

    /**
     * 获取google网站验证码
     * @return
     */
    public static String google_site_verification(){
        return site_option("google_site_verification");
    }

    /**
     * 获取百度网站验证码
     * @return
     */
    public static String baidu_site_verification(){
        return site_option("baidu_site_verification");
    }

    /**
     * 转化为年-月—日
     * @param unixTime
     * @return
     */

    public static String fmtUnixTime(Integer unixTime){
        return fmtUnixTime(unixTime, "yyyy-MM-dd");
    }

    /**
     * 转化unix时间戳
     * @param unixTime
     * @param pattern
     * @return
     */
    public static String fmtUnixTime(Integer unixTime,String pattern){
        if(unixTime != null && StringUtils.isNotBlank(pattern)){
            return DateKit.formatDateByUnixTime(unixTime,pattern);
        }
        return "";
    }

    /**
     * 英文格式的时间
     * @param unixTime
     * @return
     */
    public static String fmtUnixTime_en(Integer unixTime){
        String fmt = fmtUnixTime(unixTime, "d,MMM,yyyy");
        String[] split = fmt.split(",");
        String rs ="<span>" + split[0] + "</span>" + split[1] + "   " +split[2];
        return rs;
    }

    /**
     * 英文格式的月-日
     * @param unixTime
     * @return
     */
    public static  String fmtDate_en_yd(Integer unixTime){
        return fmtUnixTime(unixTime,"MMM d");
    }

    /**
     * 英文格式的年
     * @param unixTime
     * @return
     */
    public static String fmtDate_en_y(Integer unixTime){
        return fmtUnixTime(unixTime,"yyyy");
    }

    /**
     * 把yyyy-MM 转化为yyyy
     * @param date
     * @return
     */
    public static String paraseDate(String date){
        if(StringUtils.isNotBlank(date)){
            Date date1 = DateKit.dateFormat(date, "yyyy-MM");
            return DateKit.dateFormat(date1, "yyyy");
        }
        return null;
    }

    /**
     * 把字符串转化为Date
     * @param date
     * @return
     */
    public static Date fmtDateFromString(String date){
        if (StringUtils.isNotBlank(date)) {
            return DateKit.dateFormat(date, "yyyy-MMM");
        }
        return null;
    }

    /**
     * 把unix时间戳转化为Date;
     * @param unixTime
     * @return
     */
    public static Date parseUnixTimeTODate(Integer unixTime){
        if(unixTime != null){
            return DateKit.getDateByUnixTime(unixTime);
        }
        return null;
    }

    /**
     * 生成随机数，用于设置随机图片
     * @param max
     * @param str
     * @return
     */
    public static String random(int max,String str){
        return (UUID.random(1,max) + str);
    }
    public static String random(Long seed,int max,String str) {
        if (seed != null) {
            Random random = new Random(seed);
            return random.nextInt(max) + str;
        }
        return null;
    }
    /**
     * 获取社交的链接地址
     *
     * @return
     */
    public static Map<String, String> social() {
        final String prefix = "social_";
        Map<String, String> map = new HashMap<>();
        map.put("csdn", Webconst.initConfig.get(prefix + "csdn"));
        map.put("jianshu", Webconst.initConfig.get(prefix + "jianshu"));
        map.put("resume", Webconst.initConfig.get(prefix + "resume"));
        map.put("weibo", Webconst.initConfig.get(prefix + "weibo"));
        map.put("zhihu", Webconst.initConfig.get(prefix + "zhihu"));
        map.put("github", Webconst.initConfig.get(prefix + "github"));
        map.put("twitter", Webconst.initConfig.get(prefix + "twitter"));
        return map;
    }

    /**
     * An :grinning:awesome :smiley:string &#128516;with a few :wink:emojis!
     * <p>
     * 这种格式的字符转换为emoji表情
     * @param value
     * @return
     */

    public static String emoji(String value){
        return EmojiParser.parseToUnicode(value);
    }
    public static String gravatar(String email){
        String avatarUrl = "https://github.com/identicons/";
        if (StringUtils.isBlank(email)) {
            email = "forgetxue@163.com";

        }
        String hash = TaleUtils.MD5encode(email.trim().toLowerCase());
        return avatarUrl + hash + ".png";
    }
    public static String article(String value){
        if(StringUtils.isNotBlank(value)){
            value.replace("<!--more-->","\r\n");
            value.replace("<!-- more -->","\r\n");
            return TaleUtils.mdToHtml(value);
        }
        return null;
    }
//commons.blogPermalink(post.cid) : commons.photoPermalink(post.cid)}"

    /**
     * 返回博客的地址
     * @param cid
     * @return
     */
    public static String blogPermalink(Integer cid){
        return site_url("/blog/article/" + cid.toString());
    }
    public static String photoPermalink(Integer cid){
        return site_url("/photo/article/" + cid.toString());
    }
    public static boolean is_empty(PageInfo pageInfo){
        return pageInfo == null || pageInfo.getList() == null || (pageInfo.getList().size() == 0);
    }
    public static String substr(String str, int index){
        if(str != null)
            return str.substring(0,index);

        return null;
    }

    /**
     * 获取文章第一张图片
     *
     * @return
     */
    public static String show_thumb(String content) {
        content = TaleUtils.mdToHtml(content);
        if (content.contains("<img")) {
            String img = "";
            String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
            Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
            Matcher m_image = p_image.matcher(content);
            if (m_image.find()) {
                img = img + "," + m_image.group();
                // //匹配src
                Matcher m = Pattern.compile("src\\s*=\\s*\'?\"?(.*?)(\'|\"|>|\\s+)").matcher(img);
                if (m.find()) {
                    System.out.println("show_thumb:" + m.group(1));
                    return m.group(1);
                }
            }
        }
        return "";
    }
    public static String randomBlogPic(Long seed){
        return "/site/images/blog-images/blog-" + random( seed,12,".jpg");
    }

    public static String shiftBracket(String str){
        if(StringUtils.isNotBlank(str)){
            str = str.replace("(","");
            str = str.replace(")","");
        }
        return str;
    }
    public static String  imageUrl(String path){
        return "http://" + path;
    }

    /**
     * 获取文章中的所有图片
     * @param content
     * @return
     */
    public static List<String> show_all_thumb(String content) {
        List<String> rs = new LinkedList();
        content = TaleUtils.mdToHtml(content);
        if (content.contains("<img")) {
            String img = "";
            String regEx_img = "<[a-zA-Z]+.*?>([\\s\\S]*?)</[a-zA-Z]*>";
            Pattern p_image = Pattern.compile(regEx_img, Pattern.MULTILINE);
            Matcher m_image = p_image.matcher(content);
            while (m_image.find()) {
                String data = m_image.group(1).trim();
                if(!"".equals(data) && data.contains("<img")) {
                    System.out.println(data);
                    // //匹配src
                    Matcher m = Pattern.compile("src\\s*=\\s*\'?\"?(.*?)(\'|\"|>|\\s+)").matcher(data);
                    while (m.find()){
                        //  if (m.find()) {
                        rs.add(m.group(1));
                    }
                }

            }
        }
        return rs;
    }
    /**
     * 获取文章中所有的文字
     * @param content
     * @return
     */
    public static List<String> show_all_p(String content){
        List<String> rs = new LinkedList();
        content = TaleUtils.mdToHtml(content);
        String reg = "<[a-zA-Z]+.*?>([\\s\\S]*?)</[a-zA-Z]*>";

        Pattern p = Pattern.compile(reg, Pattern.MULTILINE);
        content = content.replace("&nbsp;", "");
        Matcher m = p.matcher(content);
        while(m.find()) {
            String data = m.group(1).trim();
            if(!"".equals(data) && !data.contains("<img")) {
                System.out.println(data);
                data = "<p>" + data + "</p>";
                rs.add(data);
            }
        }
        return rs;
    }
}
